<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片区域-- -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <!-- 输入框区域 -->
            <el-input placeholder="请输入内容" v-model="queryInfo.query" @blur='blurInput' clearable @clear='blurInput'>
                <el-button slot="append" icon="el-icon-search" @click='toSerachList'></el-button>
            </el-input>
                </el-col>
                <!-- 用户添加区域 -->
                <el-col :span="4">
                    <el-button type="primary" @click="toAddGoods">添加商品</el-button>
                </el-col>
            </el-row>
                        <!-- 用户数据表格 -->
          <el-table :data="goodsList" stripe  border>
            <el-table-column  label="#" type='index' ></el-table-column>
            <el-table-column prop="goods_name" label="商品名称" width='500px'></el-table-column>
            <el-table-column prop="goods_price" label="商品价格(元)" ></el-table-column>
            <el-table-column prop="goods_weight" label="商品重量" ></el-table-column>
            <el-table-column prop="add_time" label="创建时间">
              <template slot-scope="scopes">
                  {{scopes.row.add_time | format}}
              </template>
            </el-table-column>
            <el-table-column prop="mg_state" label="操作" >
                <!-- 状态 -->
                <template slot-scope="scope">
                <el-button type="primary"  size='mini' icon="el-icon-edit" style='width:60px' @click='toSearchList(scope.row)'></el-button>
                <el-button type="danger" size='mini' icon="el-icon-delete" style='width:60px' @click='toDeleteList(scope.row)'></el-button>
                </template>
            </el-table-column>
          </el-table>
          <!-- 分页详情 -->
          <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryInfo.pegenum" :page-sizes="[5, 10, 15, 20]" :pager-count="5"
             :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
          </div>
        </el-card>
    </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 15
      },
      //   总数
      total: 0,
      //   数据列表
      goodsList: []
    }
  },
  created () {
    //   获取商品列表
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const data = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (data.meta.status !== 200) return this.$message.error('请求数据失败')
      this.goodsList = data.data.goods
      this.total = data.data.total
    },
    // 失去焦点触发事件--获取数据
    blurInput () {
      this.getGoodsList()
    },
    // 去编辑操作商品列表
    toSearchList (val) {
    },
    toDeleteList (val) {
    // 删除列表
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$http.delete('goods/' + val.goods_id)
        this.getGoodsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 根据参数查询商品
    async toSerachList () {
      this.getGoodsList()
    },
    // 改变页面的展示的个数
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      //  重新获取数据
      this.getGoodsList()
    },
    // 改变当前页函数
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      // 重新发送请求获取页面
      this.getGoodsList()
    },
    // 跳转到添加商品页
    toAddGoods () {
      this.$router.push('/goods/add')
    }
  }

}
</script>

<style lang="less" scoped>
</style>
